#!/bin/sh
# get called every second by #listen

MAH='curl localhost:8080'
MSGCNT=`$MAH/countMessage 2>/dev/null | jq '.data'`

if [ $MSGCNT -ne 0 ]
then
    MSG=`$MAH/'fetchMessage?count=1' 2>/dev/null`

    if echo $MSG | grep '\"data\":\[\]' > /dev/null
    then # empty msg
        exit
    fi

    LOG=~/log/`~/ts`.json
    echo $MSG | json_pp -json_opt utf8,pretty > $LOG
    ~/shbot/handler $LOG
fi


# weibo
WEIBO=~/cache/lastweibo
((NEXT=`cat $WEIBO`+60))
NOW=`date +%s`
if [ $NOW -gt $NEXT ]
then
    echo $NOW > $WEIBO
    ~/shbot/weibo/weibo.sh
fi
